package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.domain.vo.CategoryBrandVO;
import com.spzx.product.service.CategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "分类品牌管理模块")
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {

    @Resource
    private CategoryBrandService categoryBrandService;
    //前端查询条件 分类id 品牌id
    @Operation(summary = "根据条件查询分类品牌的分页列表")
    @GetMapping("/list")
    public TableDataInfo list(CategoryBrand categoryBrand) {
        startPage();
        //mp只能生成单表的sql  如果需要多表联查  需要自定义sql实现
        List<CategoryBrandVO> list = categoryBrandService.listCategoryBrands(categoryBrand);
        return getDataTable(list);
    }

    @Operation(summary = "查询所有分类品牌列表")
    @GetMapping("listAll")
    public AjaxResult listAll(CategoryBrand categoryBrand){
        List<CategoryBrandVO> list = categoryBrandService.listCategoryBrands(categoryBrand);
        return success(list);
    }

    @Operation(summary = "批量删除分类品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult list(@PathVariable List<Long> ids) {
        //toAjax 可以根据增删改返回的影响记录的条数，返回成功或失败AjaxResult对象
        return toAjax(categoryBrandService.deleteBatchByIds(ids));
    }

    @Operation(summary = "新增分类单位")
    @PostMapping
    public AjaxResult add(@RequestBody CategoryBrand categoryBrand) {
        //唯一性校验:brandId和categoryId映射，组合唯一
        categoryBrandService.checkCategoryBrandUnique(categoryBrand);
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.save(categoryBrand));
    }

    //先查询详情
    @Operation(summary = "查询分类单位详情")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(categoryBrandService.getById(id));
    }

    //前端对详情进行展示 修改 提交更新请求
    @Operation(summary = "更新分类单位")
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryBrand categoryBrand) {
        //唯一性校验
        categoryBrandService.checkCategoryBrandUnique(categoryBrand);
        categoryBrand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.updateById(categoryBrand));
    }






}
